/*
 *所有关于Full_Line_Inventory类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Full_Line_InventoryService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using ATkg.BizAppProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.IRepositories;
using DGZImp.Core.DbSqlSugar;

namespace ATkg.BizAppProject.Services
{
    public partial class Full_Line_InventoryService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IFull_Line_InventoryRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Full_Line_InventoryService(
            IFull_Line_InventoryRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        //public async Task<object> GetInfoFromDb()
        //{

        //    var res = await Db.Set<Final_Product_Zhenghe>().ToListAsync();
        //    for (var i = 0; i < res.Count; i++)
        //    {
        //        var ProductGeneral = res[i].ProductGeneral;
        //        var ProductKG = res[i].ProductKG;
        //        var ProductAK = res[i].ProductAK;
        //        var ProductBF = res[i].ProductBF;

        //        //var r1 = await SyncFullLineInfo_KongGang(ProductKG);
        //        //var r2 = await SyncFullLineInfo_AiKe();
        //        //var r3 = await SyncFullLineInfo_BeiFang();
        //    }
        //    return "";
        //}

        //public async Task<object> SyncFullLineInfo_KongGang(string product_id)
        //{
        //    var qDomain = new List<object> {
        //            //网页开发者模式中Payload中的domain
        //            new List<object> { "default_code", "ilike", product_id },
        //            new List<object> { "name", "ilike", product_id }
        //        };

        //    var qFields = new List<string> {
        //            "final_incoming_qty",
        //            "final_outgoing_qty",
        //            "qty",
        //            "grade",
        //            "spec",
        //            "inventory_age"
        //        };
        //    var qPage = new
        //    {
        //        context = new { lang = "zh_CN", tz = "Asia/Shanghai" },
        //        limit = 10000,
        //        offset = 0,
        //    };

        //    var resp = await CallSearchRead("stock.quant", qDomain, qFields, qPage);

        //    if (resp.ContainsKey("error"))
        //    {
        //        var message = $"【拉取单据】{resp["error"].ToString()}";
        //        throw new AppException(message);
        //    }
        //    var res = (JArray)resp["result"];

        //    if (res == null || res.Count <= 0)
        //    {
        //        return null;
        //    }
        //    var r = new List<ResultAdd>();
        //    Console.WriteLine("空港毛坯库存批次总数：");
        //    Console.WriteLine(res.Count);

        //    for (var i = 0; i < res.Count; i++)
        //    {
        //        var x = (JObject)res[i];

        //        // 通过毛坯库获取的字段：产品、批次、数量、性能、规格
        //        // 提取 lot_id 数组的第二个元素（索引 1）
        //        string temp = x["lot_id"][1].ToString();
        //        Console.WriteLine(temp);
        //        //string lot = Regex.Replace(temp, @"\（.*?\）", "");  //去除括号里的内容，只保留批号 (（
        //        string lot = temp.Length >= 7 ? temp.Substring(0, 7) : temp;
        //        Console.WriteLine(lot);
        //        //string lot = Regex.Replace(temp, @"\(.*?\)", "");  //去除括号里的内容，只保留批号
        //        var qDomain2 = new List<object>
        //            {
        //                //网页开发者模式中Payload中的domain，需要在网页右上角设定筛选条件才有，没有的话可以根据实际情况写
        //                new List<object> { "blank_name", "=", lot}
        //            };
        //        // 通过BH检测获取的字段：常温br hcj值
        //        //需要在添加个  工厂  字段
        //        var qFields2 = new List<string>
        //            {
        //                "br_normal",
        //                "hcj_normal",
        //            };
        //        var qPage2 = new
        //        {
        //            context = new { lang = "zh_CN", tz = "Asia/Shanghai" },
        //            limit = 10000,
        //            offset = 0
        //        };
        //        var resp2 = await CallSearchReadBH("atm.qc.inspect.bh", qDomain2, qFields2, qPage2);
        //        var res2 = (JArray)resp2["result"];
        //        if (res2 == null || res2.Count <= 0)
        //        {
        //            Console.WriteLine("未找到对应批次BH值");
        //            continue;
        //        }
        //        var y = (JObject)res2[0];
        //        r.Add(new ResultAdd
        //        {
        //            ProductId = x["product_id"] is JArray ? JArray.FromObject(x["product_id"])[1].ToString().Split(new String[] { "/" }, StringSplitOptions.None)[0] : "",
        //            Lot = temp,
        //            Qty = (decimal)x["qty"],
        //            Grade = x["grade"] is JArray ? JArray.FromObject(x["grade"])[1].ToString() : "",
        //            Spec = x["spec"].ToString(),
        //            InventoryAge = (int)x["inventory_age"],
        //            Br = (decimal)y["br_normal"],
        //            Hcj = (decimal)y["hcj_normal"],
        //            Location = "空港",

        //        }); ; ;
        //        Console.WriteLine(i);

        //    }
        //    Console.WriteLine("空港毛坯库存中有BH值的批次总数：");
        //    Console.WriteLine(r.Count);
        //    return r;
        //}
    }
}
